/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.cookor.poi.modules.tenant.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cookor.poi.modules.sys.entity.Area;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.modules.tenant.entity.TTenant;
import com.cookor.poi.modules.tenant.service.TTenantService;

import java.util.List;
import java.util.Map;

/**
 * 租户Controller
 * @author poi
 * @version 2021-04-12
 */
@Controller
@RequestMapping(value = "${adminPath}/tenant/tTenant")
public class TTenantController extends BaseController {

	@Autowired
	private TTenantService tTenantService;

	@ModelAttribute
	public TTenant get(@RequestParam(required=false) String id) {
		TTenant entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tTenantService.get(id);
		}
		if (entity == null){
			entity = new TTenant();
		}
		return entity;
	}

	@RequiresPermissions("tenant:tTenant:view")
	@RequestMapping(value = {"list", ""})
	public String list(TTenant tTenant, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TTenant> page = tTenantService.findPage(new Page<TTenant>(request, response), tTenant);
		model.addAttribute("page", page);
		model.addAttribute("tTenant", tTenant);
		return "modules/tenant/tTenantList";
	}

	@RequiresPermissions("tenant:tTenant:view")
	@RequestMapping(value = "form")
	public String form(TTenant tTenant, Model model) {
		model.addAttribute("tTenant", tTenant);
		return "modules/tenant/tTenantForm";
	}

	@RequiresPermissions("tenant:tTenant:edit")
	@RequestMapping(value = "save")
	public String save(TTenant tTenant, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tTenant)){
			return form(tTenant, model);
		}
		tTenantService.save(tTenant);
		addMessage(redirectAttributes, "保存租户成功");
		return "redirect:"+Global.getAdminPath()+"/tenant/tTenant/?repage";
	}

	@RequiresPermissions("tenant:tTenant:edit")
	@RequestMapping(value = "delete")
	public String delete(TTenant tTenant, RedirectAttributes redirectAttributes) {
		tTenantService.delete(tTenant);
		addMessage(redirectAttributes, "删除租户成功");
		return "redirect:"+Global.getAdminPath()+"/tenant/tTenant/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String tenantName, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		TTenant tTenant = new TTenant();
		tTenant.setTenantName(tenantName);
		List<TTenant> list = tTenantService.findList(tTenant);
		TTenant e;
		Map<String, Object> map;
		for (int i=0; i<list.size(); i++){
			e = list.get(i);
			map = Maps.newHashMap();
			map.put("id", e.getId());
			map.put("name", e.getTenantName());
			mapList.add(map);
		}
		return mapList;
	}

}
